/*
 * Created by JFormDesigner on Thu Dec 12 22:26:46 CST 2024
 */

package com.hafu.view.ShangPin;

import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class AddShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public AddShangPinFrm() {
        initComponents();
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 增加商品
        String name = textField1.getText();
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名称不能为空！");
            return;
        }
        if (StringUtil.isEmpty(textField2.getText())){
            JOptionPane.showMessageDialog(null,"商品进价不能为空！");
            return;
        }
        if (StringUtil.isEmpty(textField3.getText())){
            JOptionPane.showMessageDialog(null,"商品售价不能为空！");
            return;
        }
        BigDecimal price = new BigDecimal(textField2.getText());
        BigDecimal sellPrice = new BigDecimal(textField3.getText());
        SpInfo spInfo = new SpInfo(name,price,sellPrice);
        Boolean aBoolean = spInfoService.addSpInfo(spInfo);
        if (aBoolean){
            JOptionPane.showMessageDialog(null,"添加商品成功！");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"添加商品失败！");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5546\u54c1");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5546\u54c1\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- label3 ----
        label3.setText("\u5546\u54c1\u552e\u4ef7");

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(73, 73, 73)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                    .addComponent(label2)
                                    .addComponent(label1))
                                .addComponent(label3))
                            .addGap(115, 115, 115)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                .addComponent(textField1, GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(218, 218, 218)
                            .addComponent(button1)))
                    .addContainerGap(321, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(54, 54, 54)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(53, 53, 53)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(69, 69, 69)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label3)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(99, 99, 99)
                    .addComponent(button1)
                    .addContainerGap(128, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
